let tableIns;
let tree;
let startDateIns;
let endDateIns;
layui.use(['element', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    let form = layui.form;//select、单选、复选等依赖form
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let laydate = layui.laydate;
    tree = layui.tree;
    var $=layui.jquery;
    //初始化select多选
    var xmSelect = layui.xmSelect;
    let height = document.documentElement.clientHeight - 60;
    
    let demo;
    
    $.post('/platform/grant/findByQueryTime', {}, function (data) {
     // 渲染多选下拉框
        let res = data.data;
        var dats =[];
        for( i=0;i<res.length;i++){
        	dats.push({name: res[i].createTime, value: res[i].createTime});
        }
       demo = xmSelect.render({
            el:'#time',
            data:dats
        });
       
    });

    tableIns = table.render({
        elem: '#quarterListTable'
        , url: ctx + '/platform/grant/queryByName'
        , method: 'GET'
        //请求前参数处理
        , where: {
            payeeName: ""
            , serviceName: ""
            , time: ""
            , cardCode : ""
            ,startaxAmount : ""
            ,endtaxAmount : ""
            ,startaxMoney : ""
            ,endtaxMoney  : ""
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            var sum=0;
            var taxSum=0;
            var renshu=0;
            var buhanshui=0;
            data.forEach(function(item, index){
                
            })
           $.each(data,function(i,obj){
        	   if(obj.cardCode != null && obj.cardCode != ''){
        		   renshu+=1;
        	   }
        	   	buhanshui+=obj.taxAmount;
                sum+=obj.payAmount;
                taxSum+=obj.taxMoney;
            });
            $("#sum_money3").html("人数："+renshu);
            $("#sum_money1").html("发放总金额："+formatMoney(sum));
            $("#sum_money2").html("个税总金额："+formatMoney(taxSum));
            $("#sum_money4").html("不含税总金额："+formatMoney(buhanshui));
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data  //解析数据列表
            };
        }
        ,toolbar:true
        ,title: '用户个税数据表'
        ,defaultToolbar: ['filter', 'exports', 'print']
        , cols: [[
            /*{field: 'orderId', width:200, title: '订单号'},*/
             /*{field: 'id',width:200,title: '流水号'}
            , *///{field: 'sellerName',title: '商户名称'}
             {field: 'serviceName',title: '服务商名称'}
            , {field: 'personName',title: '收款人姓名'}
            , {field: 'cardCode',title: '身份证号'}
            , {field: 'payAmount',title: '含税金额', align:'right', style:'font-weight:bold',
                templet: function(d){return formatMoney(d.payAmount);}}
            ,{field: 'taxAmount',title: '增值税', align:'right', style:'font-weight:bold',
                templet: function(d){return formatMoney(d.taxAmount);}}
            , {field: 'taxPercent',title: '增值税', templet: function(d){return d.taxPercent+"%";}}
            , {field: 'tax',title: '个税征收率', templet: function(d){return d.tax+"%";}}
            , {field: 'taxMoney',title: '税额（个税）', align:'right', style:'font-weight:bold', }
            , {field: 'createTime', width:180, title: '请求时间'}
           /* , {fixed: 'right', width:100, title: '状态', toolbar: '#quarterListTableBar'}*/
        ]]
       /* , page: true*/
        , height: height
        , cellMinWidth: 80
    });

    var $=layui.jquery;
    $(".time_btn").click(function(){
        $(".time_btn").addClass("layui-btn-radius").addClass("layui-btn-primary");
        $(this).removeClass("layui-btn-radius").removeClass("layui-btn-primary");
        $(".time_btn").removeClass("checked_btn");
        $(this).addClass("checked_btn");
        if($(this).html()=="自定义"){
            $("#queryByStartDate").removeClass("layui-hide");
            $("#queryByEndDate").removeClass("layui-hide");
        }else{
            $("#queryByStartDate").addClass("layui-hide");
            $("#queryByEndDate").addClass("layui-hide");
        }
    });

    $("#queryBtn,.click_btn").click(function(){
    	 var payeeName=$("#queryUserName").val();
    	 let queryByAgentName = $("#queryByAgentName").val();
    	 var cardCode=$("#cardCode").val();
    	 var startaxAmount=$("#startaxAmount").val();
    	 var endtaxAmount=$("#endtaxAmount").val();
    	 var startaxMoney=$("#startaxMoney").val();
    	 var endtaxMoney=$("#endtaxMoney").val();
    	 var str=demo.getValue();
    	 var time="";
    	 for(i=0;i<str.length;i++){
    		 time+=str[i].value + ',';
    	 }
    	 time=time.substring(0,time.length-1);
        table.reload('quarterListTable', {
            url:'/platform/grant/queryByName'
            , where: {
                'payeeName':payeeName,
                'serviceName':queryByAgentName,
                'time':time
                ,cardCode : cardCode
                ,startaxAmount : startaxAmount
                ,endtaxAmount : endtaxAmount
                ,startaxMoney : startaxMoney
                ,endtaxMoney  : endtaxMoney
            }
            , page: {
                curr: 1
            }
        });

    })


    //日期选择器
    startDateIns = laydate.render({
        elem: '#queryByStartDate',
        format: "yyyy-MM",
        type: 'month',
        trigger: 'click'
    });

    //日期选择器
    endDateIns = laydate.render({
        elem: '#queryByEndDate',
        format: "yyyy-MM",
        type: 'month',
        trigger: 'click'
    });
});

